package com.aj.http.response;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;

import com.aj.databases.entry.AlbumPhotoInfo;
import com.aj.databases.entry.BabyPhotoArrayInfo;
import com.aj.databases.entry.BabyPhotoInfo;

@SuppressLint("NewApi")
public class BabyPhotoResponse extends BaseRespond {

	private BabyPhotoArrayInfo babyPhotoArrayInfo;

	public BabyPhotoResponse(JSONObject json) {
		super(json);
		if (common != null && common.getResult() != null) {
			try {
				if (common.getResult() == null) {
					return;
				}
				babyPhotoArrayInfo = new BabyPhotoArrayInfo();
				JSONObject jsonObject = common.getResult();
				if (!jsonObject.isNull("babyId")) {
					babyPhotoArrayInfo
							.setBabyId(jsonObject.getString("babyId"));
				}
				if (!jsonObject.isNull("babyCallName")) {
					babyPhotoArrayInfo.setBabyCallName(jsonObject
							.getString("babyCallName"));
				}
				if (!jsonObject.isNull("babyName")) {
					babyPhotoArrayInfo.setBabyName(jsonObject
							.getString("babyName"));
				}
				if (!jsonObject.isNull("babyPhoto")) {
					babyPhotoArrayInfo.setBabyPhoto(jsonObject
							.getString("babyPhoto"));
				}
				if (!jsonObject.isNull("babyAge")) {
					babyPhotoArrayInfo.setBabyAge(jsonObject
							.getString("babyAge"));
				}

				if (!jsonObject.isNull("photoList")) {
					JSONArray jsonArray = jsonObject.getJSONArray("photoList");
					BabyPhotoInfo babyPhotoInfo;
					List<BabyPhotoInfo> babyPhotoInfoList = new ArrayList<BabyPhotoInfo>();
					for (int i = 0; i < jsonArray.length(); i++) {
						JSONObject tempJsonObject = jsonArray.getJSONObject(i);
						babyPhotoInfo = new BabyPhotoInfo();
						babyPhotoInfo.setPhotoAge(tempJsonObject
								.getString("photoAge"));
						if (!tempJsonObject.isNull("photos")) {
							JSONArray tempJsonArray = tempJsonObject
									.getJSONArray("photos");
							List<AlbumPhotoInfo> albumPhotoInfoList = new ArrayList<AlbumPhotoInfo>();
							for (int j = 0; j < tempJsonArray.length(); j++) {
								JSONObject oneJsonObject = tempJsonArray
										.getJSONObject(j);
								AlbumPhotoInfo albumPhotoInfo = new AlbumPhotoInfo();
								albumPhotoInfo.setAlbumId(oneJsonObject
										.getString("photoId"));
								albumPhotoInfo.setAlbumUrl(oneJsonObject
										.getString("photoUrl"));
								albumPhotoInfoList.add(albumPhotoInfo);
							}
							babyPhotoInfo
									.setAlbumPhotoInfoList(albumPhotoInfoList);
							babyPhotoInfoList.add(babyPhotoInfo);
						}
					}
					babyPhotoArrayInfo.setPrayLetterInfoList(babyPhotoInfoList);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	public BabyPhotoArrayInfo getBabyPhotoArrayInfo() {
		return babyPhotoArrayInfo;
	}

	public void setBabyPhotoArrayInfo(BabyPhotoArrayInfo babyPhotoArrayInfo) {
		this.babyPhotoArrayInfo = babyPhotoArrayInfo;
	}

}
